package com.chen.authority.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.authority.common.constant.SysConstant;
import com.chen.authority.common.model.PageQo;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.util.WrapperUtil;
import com.chen.authority.model.dto.NoticeDto;
import com.chen.authority.model.po.NoticePo;
import com.chen.authority.model.qo.NoticeQo;
import com.chen.authority.model.vo.NoticeVo;
import com.chen.authority.service.NoticeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.chen.authority.mapper.NoticeMapper;

/**
 * 通知公告Service业务层处理
 *
 * @author chengy
 * @date 2022-08-17
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, NoticePo> implements NoticeService {

    @Override
    public Response addOrUpdateNotice(Integer id, NoticeDto dto) {
        if (dto.getType() == SysConstant.TONG_ZHI && StringUtils.isNotBlank(dto.getContent())) {
            return Response.fail("通知内容必须为空");
        }
        if (dto.getType() == SysConstant.GONG_GAO && StringUtils.isBlank(dto.getContent())) {
            return Response.fail("公告内容不能为空");
        }
        NoticePo notice = dto.toPo(NoticePo.class);
        notice.setId(id);
        return this.saveOrUpdate(notice) ? Response.success() : Response.fail();
    }

    @Override
    public Page<NoticeVo> pageQueryNoticeList(PageQo pageQo, NoticeQo noticeQo) {
        return baseMapper.selectVoPage(pageQo.build(), WrapperUtil.buildQueryWrapper(noticeQo));
    }

}